﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using System.Windows.Browser;

namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 初始数据主表  
    /// </summary>
    /// Title:ZMCM50
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 李红星
    /// Version: 1.0
    /// History:
    ///       2011-6-27 9:42:56  李红星   [创建]
    ///
    public partial class ZMCM50 : EFViewCmes
    {
        public ZMCM50()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            String fdKeyId = "空",fdStatus="草拟";
            if (null != this.grid.SelectedItem)
            {
                fdKeyId = (String)this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null);
                fdStatus = (String)this.grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.grid.SelectedItem, null);
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdInventoryId", fdKeyId.ToString());
                args.Add("fdStatus", fdStatus.ToString());
                args.Add("fdDepotId", (String)this.grid.SelectedItem.GetType().GetProperty("fdDepotId").GetValue(this.grid.SelectedItem, null));
                this.container.LoadView("ZMCM51", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        /// <summary>
        /// 盘点管理新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            //生成
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM51");
            eiInfo.Set("method", "queryNoteCode");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompletedQueryNote);      
        }

        private void OnDoWorkCompletedQueryNote(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                this.OpenPopup(inInfo, "盘点管理-新增", "insert", null);
            }
        }

        /// <summary>
        /// 盘点管理修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            bool canDelete = true;
            String msg = "";
            if (_list.Count <= 0)
            {
                msg = "请选择一条记录进行修改！";
                canDelete = false;
            }
            else
            {
                     if (this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals("生效"))
                    {
                        msg = "选择的记录已经生效，不允许修改！";
                        canDelete = false;
                    }
            }
            if(!canDelete)
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }

            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "盘点管理-修改", "update", null, sender, e);
        }


        /// <summary>
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            Inventory inventory = new Inventory(dialogType);//构造一个新的修改页面
            inventory.EIDataContext = new Platform.Core.UI.EIDataContext();
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.depotInfo"));
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.empName"));
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.deptName"));

            if (dialogType.Equals("insert"))
            {
                inventory.fdInventoryCode.Value = eIInfo.Get("fdNoteCode").ToString();
            }

            (inventory.fdEditor.Content as CmesCustomView).Value = this.EIDataContext.GetAttribute("userId");
            (inventory.fdEditor.Content as CmesCustomView).Display = this.EIDataContext.GetAttribute("userName");
            inventory.fdInventoryDate.Value = DateTime.Now.ToString("yyyy-MM-dd"); //盘点日期默认当前日期
            inventory.fdEditDate.Value = DateTime.Now.ToString("yyyy-MM-dd"); //编制日期默认当前日期
            inventory.EIDataContext.Update(eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = inventory;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();
        }
        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }

        /// <summary>
        /// 盘点管理删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIRow row = new EIRow();
            bool canDelete = true;
            String msg = "";
            if (_list.Count <= 0)
            {
                msg = "请选择要删除的记录(一条或多条)！";
                canDelete = false;
            }
            else
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    if (this.grid.GetCheckedEIRows()[i]["fdStatus"].ToString().Equals("生效"))
                    {
                        msg = "选择的记录已经生效或包含有已经生效的记录，不允许删除！";
                        canDelete = false;
                        break;
                    }
                }
            }
            if (canDelete)
            {

                base.OnDeleteButtonClick("ZMCM50", "delete", sender, e);
            }
            else
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }
        }

        /// <summary>
        /// 盘点管理生效操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnEffectButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            (sender as Button).IsEnabled = false;
            EIInfo eiInfo = new EIInfo();
            EIRow row = new EIRow();
            if (_list.Count <= 0) 
            {
                this.OpenEFWindowAlert("请选择一条记录进行生效！", true);
                return;
            }else if (_list.Count > 1)
            {
                this.OpenEFWindowAlert("一次只能生效一条记录！", true);
                return;
            }
            if (this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals("草拟"))
            {

                base.OnModifyButtonClick("ZMCM50", "confirm", "生效", sender, e);
            }
            else
            {
                base.OpenEFWindowAlert("盘点单已经生效，不能重复生效！", true);
            }
        }

        /// <summary>
        /// 打印盘点操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行打印！", true);
                return;
            }
            else if (count != 1)
            {
                this.OpenEFWindowAlert("只能选择一条记录进行打印！", true);
                return;
            }

            if (!this.grid.GetCheckedEIRows()[0]["fdDepotId"].Equals("01"))
            {
                this.OpenEFWindowAlert("当前打印的盘点单不是一级库存盘点单，无法提供打印功能！", true);
                return;
            }
            string fdKeyId = this.grid.GetCheckedEIRows()[0]["fdKeyId"].ToString();
            string date = this.grid.GetCheckedEIRows()[0]["fdInventoryDate"].ToString();
            string year = this.grid.GetCheckedEIRows()[0]["fdInventoryDate"].ToString().Substring(0,4);
            string month = this.grid.GetCheckedEIRows()[0]["fdInventoryDate"].ToString().Substring(5, 1).Equals("0") ? this.grid.GetCheckedEIRows()[0]["fdInventoryDate"].ToString().Substring(6, 1) : this.grid.GetCheckedEIRows()[0]["fdInventoryDate"].ToString().Substring(5, 2);
            string qYear = (Convert.ToInt32(year)-1).ToString();
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=report_PD&PD_KeyId=" + fdKeyId + "&PD_Year=" + year + "&PD_Month=" + month + "&PD_Qyear=" + qYear + "&PD_Date=" + date);
            HtmlPage.Window.Navigate(uri, "_blank");
            base.TempButtonOpen(true);//按钮生效
        }
    }
}
